using System;
using System.Data;
using System.Data.SqlTypes;
using System.Data.SqlClient;

namespace Model
{
	public partial class Tbl010_Currency : DBInteractionBase
	{
		#region Class Member Declarations
			private SqlString		_curBuying, _curSelling;
			private SqlInt32		_curID;
			private SqlString		_currency;
		#endregion


		public Tbl010_Currency()
		{
			// Nothing for now.
		}


		public override bool Insert()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl010_Currency_Insert]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@CurID", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _curID));
				cmdToExecute.Parameters.Add(new SqlParameter("@Currency", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _currency));
				cmdToExecute.Parameters.Add(new SqlParameter("@CurBuying", SqlDbType.Char, 10, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _curBuying));
				cmdToExecute.Parameters.Add(new SqlParameter("@CurSelling", SqlDbType.Char, 10, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _curSelling));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl010_Currency::Insert::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
			}
		}


		public override bool Update()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl010_Currency_Update]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@CurID", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _curID));
				cmdToExecute.Parameters.Add(new SqlParameter("@Currency", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _currency));
				cmdToExecute.Parameters.Add(new SqlParameter("@CurBuying", SqlDbType.Char, 10, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _curBuying));
				cmdToExecute.Parameters.Add(new SqlParameter("@CurSelling", SqlDbType.Char, 10, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _curSelling));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl010_Currency::Update::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
			}
		}


		public override bool Delete()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl010_Currency_Delete]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@CurID", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _curID));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl010_Currency::Delete::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
			}
		}


		public override DataTable SelectOne()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl010_Currency_SelectOne]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;
			DataTable toReturn = new DataTable("tbl010_Currency");
			SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@CurID", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _curID));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				adapter.Fill(toReturn);
				if(toReturn.Rows.Count > 0)
				{
					_curID = (Int32)toReturn.Rows[0]["CurID"];
					_currency = toReturn.Rows[0]["Currency"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["Currency"];
					_curBuying = toReturn.Rows[0]["CurBuying"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["CurBuying"];
					_curSelling = toReturn.Rows[0]["CurSelling"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["CurSelling"];
				}
				return toReturn;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl010_Currency::SelectOne::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
				adapter.Dispose();
			}
		}


		public override DataTable SelectAll()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl010_Currency_SelectAll]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;
			DataTable toReturn = new DataTable("tbl010_Currency");
			SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				adapter.Fill(toReturn);
				return toReturn;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl010_Currency::SelectAll::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
				adapter.Dispose();
			}
		}


		#region Class Property Declarations
		public SqlInt32 CurID
		{
			get
			{
				return _curID;
			}
			set
			{
				SqlInt32 curIDTmp = (SqlInt32)value;
				if(curIDTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("CurID", "CurID can't be NULL");
				}
				_curID = value;
			}
		}


		public SqlString Currency
		{
			get
			{
				return _currency;
			}
			set
			{
				_currency = value;
			}
		}


		public SqlString CurBuying
		{
			get
			{
				return _curBuying;
			}
			set
			{
				_curBuying = value;
			}
		}


		public SqlString CurSelling
		{
			get
			{
				return _curSelling;
			}
			set
			{
				_curSelling = value;
			}
		}
		#endregion
	}
}
